package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import fr.bordeaux1.planingdesetudiants.model.Cours;
import fr.bordeaux1.planingdesetudiants.model.Enseignant;
import fr.bordeaux1.planingdesetudiants.model.Groupe;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class ListerCours extends Composite {
	private IFenetrePrincipale fenetrePrincipale;
	private Combo listeUE;
	private Combo listeCours;
	private Combo listeProf;
	private Text textSalle;
	private Table listGpe;
	private Combo coursExistant;

	public ListerCours(Composite arg0, int arg1,
			IFenetrePrincipale fenetrePrincipale) {
		super(arg0, arg1);
		this.fenetrePrincipale = fenetrePrincipale;
		this.setLayout(new FormLayout());

		Label nameE = new Label(this, SWT.NONE);
		FormData fd_nameE = new FormData();
		fd_nameE.left = new FormAttachment(0, 10);
		fd_nameE.top = new FormAttachment(0, 10);
		nameE.setLayoutData(fd_nameE);
		nameE.setText("Selectionner l'UE du cours à modifier:");

		listeUE = new Combo(this, SWT.BORDER | SWT.READ_ONLY);
		fd_nameE.right = new FormAttachment(listeUE, 0,5);
		FormData fd_listeUE = new FormData();
		fd_listeUE.left = new FormAttachment(50, 10);
		fd_listeUE.right = new FormAttachment(100, -10);
		fd_listeUE.top = new FormAttachment(0, 5);

		listeUE.setLayoutData(fd_listeUE);
		java.util.List<Ue> res;
		try {
			res = fenetrePrincipale.getDb().listerUes();

			for (Ue ue : res) {
				listeUE.add(ue.getCode() + " " + ue.getLibelle());
				listeUE.setData(ue.getCode() + " " + ue.getLibelle(), ue);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		listeUE.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					listeCours.removeAll();
					Ue ueSelected = (Ue) listeUE.getData(listeUE
							.getItem(listeUE.getSelectionIndex()));
					java.util.List<Cours> cours;

					cours = ListerCours.this.fenetrePrincipale.getDb()
							.listerCours();

					for (Cours cour : cours) {
						if (cour.getCodeUe().equals(ueSelected.getCode())) {
							coursExistant.add(cour.getType() + cour.getId());
							coursExistant.setData(cour.getType()+ cour.getId(),cour);
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
		/*
		 * Choix du Cours ICI
		 */

		Label cour = new Label(this, SWT.NONE);
		FormData fd_lblCours = new FormData();
		fd_lblCours.top = new FormAttachment(listeUE, 10);
		fd_lblCours.right = new FormAttachment(50, 0);
		fd_lblCours.left = new FormAttachment(20, 0);
		cour.setLayoutData(fd_lblCours);
		cour.setText("Cours à modifier: ");

		coursExistant = new Combo(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_coursExistant = new FormData();
		fd_coursExistant.top = new FormAttachment(listeUE, 10);
		fd_coursExistant.right = new FormAttachment(100, -10);
		fd_coursExistant.left = new FormAttachment(cour, 10);
		coursExistant.setLayoutData(fd_coursExistant);
		coursExistant.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					listGpe.removeAll();
					listeProf.removeAll();
					Cours courSelected = (Cours) coursExistant.getData(coursExistant.getItem(coursExistant.getSelectionIndex()));
					/*
					 * Type
					 */
					listeCours.setText(courSelected.getType());

					/*
					 *  listeProf
					 */
					java.util.List<Enseignant> profs;
					profs = ListerCours.this.fenetrePrincipale.getDb()
							.listerEnseignant();
					listeProf.add("Non renseigné");
					listeProf.select(0);
					for (Enseignant current : profs) {
						listeProf.add(current.getNom() + " "
								+ current.getPrenom());
						listeProf.setData(current.getNom() + " "
								+ current.getPrenom(), current);
						if (current.getId() == courSelected.getIdEnseignant()){
							listeProf.select(profs.indexOf(current)+1);
						}
					}

					/*
					 * Salle
					 */
					textSalle.setText(courSelected.getSalle());

					/*
					 * ListGpe
					 */
					java.util.List<Groupe> grps, grpsCheck;
					grps = ListerCours.this.fenetrePrincipale.getDb().listerGroupes();
					grpsCheck = ListerCours.this.fenetrePrincipale.getDb().listerGroupesDeCours(courSelected);
					for (Groupe current : grps) {
						if (current.getCodeUe().equals(courSelected.getCodeUe())){
							TableItem item = new TableItem(listGpe, SWT.CHECK
									| SWT.V_SCROLL | SWT.H_SCROLL);
							item.setData(current);
							item.setText(current.getNom());
							for (Groupe grp : grpsCheck) {
								if (grp.getId() == current.getId())
									item.setChecked(true);
							}
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});

		Label type = new Label(this, SWT.NONE);
		FormData fd_lblType = new FormData();
		fd_lblType.top = new FormAttachment(15, 21);
		fd_lblType.left = new FormAttachment(20, 0);
		fd_lblType.right = new FormAttachment(50, 0);
		type.setLayoutData(fd_lblType);
		type.setText("Type");

		listeCours = new Combo(this, SWT.BORDER);
		FormData fd_listeCours = new FormData();
		fd_listeCours.left = new FormAttachment(type, 10);
		fd_listeCours.top = new FormAttachment(15, 20);
		fd_listeCours.right = new FormAttachment(100, -10);
		listeCours.setLayoutData(fd_listeCours);
		listeCours.add(Cours.TYPE_COURS_MAGISTRAL);
		listeCours.add(Cours.TYPE_TD);
		listeCours.add(Cours.TYPE_TP);

		Label prof = new Label(this, SWT.NONE);
		FormData fd_prof = new FormData();
		fd_prof.top = new FormAttachment(listeCours, 10);
		fd_prof.left = new FormAttachment(20, 0);
		fd_prof.right = new FormAttachment(50, 0);
		prof.setLayoutData(fd_prof);
		prof.setText("Enseignant :");

		listeProf = new Combo(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_listeProf = new FormData();
		fd_listeProf.top = new FormAttachment(listeCours, 10);
		fd_listeProf.left = new FormAttachment(prof, 10);
		fd_listeProf.right = new FormAttachment(100, -10);
		listeProf.setLayoutData(fd_listeProf);
		listeProf.add("Non renseigné");
		try {
			java.util.List<Enseignant> resu = fenetrePrincipale.getDb()
					.listerEnseignant();
			for (Enseignant enseignant : resu) {
				listeProf.add(enseignant.getNom() + " "
						+ enseignant.getPrenom());
				listeProf.setData(
						enseignant.getNom() + " " + enseignant.getPrenom(),
						enseignant);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Label salle = new Label(this, SWT.NONE);
		FormData fd_salle = new FormData();
		fd_salle.top = new FormAttachment(listeProf, 10);
		fd_salle.left = new FormAttachment(20, 0);
		fd_salle.right = new FormAttachment(50, 0);
		salle.setLayoutData(fd_salle);
		salle.setText("Salle :");

		textSalle = new Text(this, SWT.BORDER);
		FormData fd_textSalle = new FormData();
		fd_textSalle.top = new FormAttachment(listeProf, 15);
		fd_textSalle.left = new FormAttachment(salle, 10);
		fd_textSalle.right = new FormAttachment(100, -10);
		textSalle.setLayoutData(fd_textSalle);

		Label group = new Label(this, SWT.NONE);
		group.setText("Groupes :");
		FormData fd_group = new FormData();
		fd_group.top = new FormAttachment(textSalle, 10);
		fd_group.right = new FormAttachment(50, 25);
		fd_group.left = new FormAttachment(50, -25);
		group.setLayoutData(fd_group);

		listGpe = new Table(this, SWT.CHECK);
		FormData fd_listGpe = new FormData();
		fd_listGpe.top = new FormAttachment(group, 10);
		fd_listGpe.right = new FormAttachment(50, 100);
		fd_listGpe.left = new FormAttachment(50, -100);
		fd_listGpe.bottom = new FormAttachment(90, 0);
		listGpe.setLayoutData(fd_listGpe);
		listGpe.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (arg0.detail == SWT.CHECK) {
					Cours courSelected = (Cours) coursExistant.getData(coursExistant.getItem(coursExistant.getSelectionIndex()));
					TableItem item = (TableItem) arg0.item;					
					Groupe grp = (Groupe) item.getData();

					try {
						if (item.getChecked()) {
							ListerCours.this.fenetrePrincipale.getDb()
							.ajouterGroupeAuCours(grp, courSelected);
						} else {
							ListerCours.this.fenetrePrincipale.getDb()
							.retirerGroupeDeCours(grp, courSelected);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});

		Button ok = new Button(this, SWT.NONE);
		FormData fd_ok = new FormData();
		fd_ok.top = new FormAttachment(listGpe,20);
		fd_ok.left = new FormAttachment(50,-75);
		fd_ok.right = new FormAttachment(50,75);
		fd_ok.bottom = new FormAttachment(100,-5);
		ok.setLayoutData(fd_ok);
		ok.setText("Modifier le cours");
		ok.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					Cours courSelected = (Cours) coursExistant.getData(coursExistant.getItem(coursExistant.getSelectionIndex()));
					courSelected.setSalle(textSalle.getText());
					courSelected.setType(listeCours.getText());
					if (!(listeProf.getText().equalsIgnoreCase("Non renseigné") || listeProf.getSelectionIndex()== -1)){
						Enseignant prof = (Enseignant) listeProf.getData(listeProf.getItem(listeProf.getSelectionIndex()));
						courSelected.setIdEnseignant(prof.getId());
					}
					ListerCours.this.fenetrePrincipale.getDb().modifierCours(courSelected);
					ListerCours.this.fenetrePrincipale.updateStatusBar("Cours modifié avec succès");
					listeCours.removeAll();
					listeUE.deselectAll();
					textSalle.setText("");
					coursExistant.removeAll();
					listGpe.removeAll();
					listeProf.removeAll();
					ListerCours.this.fenetrePrincipale.modificationApportees();
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}

		});

	}
}
